// pages/detail-page/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:null,
    default:[],
    userdata:[],
    quantity:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   this.setData({id:options.id})
   let that=this
   wx.request({
    url: 'http://172.20.10.2',
    dataType: 'json',
    success(res) {
        let hh= res.data[0].data.some((result,index)=>{
         if(result.id==that.data.id){
          var a=result.userevaluate[0]
          if(a.name!='匿名用户'){
            var len=a.name.length-1-1
            var xing=''
            for(var i=0;i<len;i++){
              xing+='*'
            }
            var shishi= a.name.substring(0,1)+xing+a.name.substring(a.name.length-1);
            console.log(shishi);
            a.name= shishi
          }
          that.setData({default:result})
           return result.id==that.data.id
         }
       })
       if(hh){

       }else{
        res.data[1].data.some((result,index)=>{
          if(result.id==that.data.id){
            var a=result.userevaluate[0]
          if(a.name!='匿名用户'){
            var len=a.name.length-1-1
            var xing=''
            for(var i=0;i<len;i++){
              xing+='*'
            }
            var shishi= a.name.substring(0,1)+xing+a.name.substring(a.name.length-1);
            a.name= shishi
          }
            that.setData({default:result})
          }
        })
       }
    }
  })
  try {
    var value = JSON.parse(wx.getStorageSync('shopping'))
    if (value) {
      this.setData({quantity:value.length})
    }
  } catch (e) {
    // Do something when catch error
  }
  },
  swiperChange:function(e) {     
    this.setData({
      currentIndex: e.detail.current
    })      
  },
  bucuo:function(e){
    let shishi=e.currentTarget.dataset.userevaluate
    wx.navigateTo({
      url: `../detail-usere/index?comment=${encodeURIComponent(JSON.stringify(shishi))}`,
    })
  },
  mairu:function(e){
    let ke=e.currentTarget.dataset.commodity
    wx.navigateTo({
      url:"../detail-purchase/index?item="+encodeURIComponent(JSON.stringify(ke))
    })
  },
  addtocart:function(){
    try {
      var value = JSON.parse(wx.getStorageSync('shopping'))
      if (value) {
        console.log(value);
        var xiaole= value.some((res,index)=>{
          console.log(res.id,this.data.default.id);
         return res.id==this.data.default.id
        })
        if(xiaole){
          console.log('222');
          wx.showToast({
            title: '购物车里已有',
            icon: 'success',
            duration: 2000
          })
        }else{
          let xiao= value.map((res,index)=>{
            console.log(res);
            res.Selected=true
            return res
          })
          let kexiao= this.data.default.Selected=true
          console.log(kexiao);
          xiao.push(this.data.default)
          this.setData({quantity:xiao.length})
          wx.setStorageSync('shopping',JSON.stringify(xiao))
        }
      }
    } catch (e) {
      console.log(this.data.default.Selected);
      let xiao= this.data.default.Selected=true
      console.log(xiao);
      this.data.userdata.push(this.data.default)
      this.setData({quantity:this.data.userdata.length})
      wx.setStorageSync('shopping',JSON.stringify(this.data.userdata))
    }
  }
})